package cn.pconline.autoclub.util; 

import cn.pconline.autoclub.config.SystemConfig;
import cn.pconline.autoclub.util.ubb.*;
import cn.pconline.autoclub.util.ubb.node.*;
import java.util.HashMap;
import java.util.Map;


/** 
 * @author 作者 guoqiang
 * @version 2014-2-14
 */
public class UBB2 {
static Map<String, Node> builderMap = new HashMap<String, Node>();	//UBB节点键值对
    
    private SystemConfig systemConfig;		//系统配置

    public void setSystemConfig(SystemConfig systemConfig) {
        this.systemConfig = systemConfig;
    }

	/**
	 * 初始化节点，每添加一个节点，表示UBB支持该节点的与HTML的转换
	 */
    public void init() {
        EmotNode emotNode = new EmotNode();
        emotNode.setEmotRoot(systemConfig.getEmotRoot());
        builderMap.put("emot", emotNode);

        UrlNode urlNode = new UrlNode();
//		urlNode.setAttachRoot(systemConfig.getAttachmentRoot());
        builderMap.put("url", urlNode);
        builderMap.put("img", new ImgNode());
        builderMap.put("email", new EmailNode());
        builderMap.put("quote", new QuoteNode());
        builderMap.put("code", new CodeNode());

        HideNode hideNodeFormatter = new HideNode();
        builderMap.put("hide", hideNodeFormatter);
        builderMap.put("_hide_", hideNodeFormatter);

        SimpleNode simpleNodeFormatter = new SimpleNode();
        builderMap.put("b", simpleNodeFormatter);
        builderMap.put("i", simpleNodeFormatter);
        builderMap.put("u", simpleNodeFormatter);

        builderMap.put("flash", new FlashNode());
        builderMap.put("rm", new RmNode());
        builderMap.put("wma", new WmaNode());
        builderMap.put("wmv", new WmvNode());
        builderMap.put("media", new MediaNode());
        builderMap.put("audio", new AudioNode());

        builderMap.put("list", new ListNode());
        builderMap.put("table", new TableNode());
        builderMap.put("tr", new TrNode());
        builderMap.put("td", new TdNode());

        SimpleNode ignoreNodeFormatter = new SimpleNode();
        builderMap.put("fly", ignoreNodeFormatter);
        builderMap.put("move", ignoreNodeFormatter);
        builderMap.put("shadow", ignoreNodeFormatter);
        builderMap.put("glow", ignoreNodeFormatter);

        FontNode fontNodeFormatter = new FontNode();
        builderMap.put("font", fontNodeFormatter);
        builderMap.put("face", fontNodeFormatter);
        builderMap.put("color", fontNodeFormatter);
        builderMap.put("size", fontNodeFormatter);

        builderMap.put("align", new AlignNode());
        builderMap.put("center", new CenterNode());
    }

	/**
	 * UBB代码转HTML代码
	 * @param ubb	UBB文本
	 * @return HTML文本
	 */
    public static String ubb2html(String ubb) {
        UBBHandler handler = new UBBHandler();
        handler.setBuilderMap(builderMap);
        UBBParser.parse(ubb, handler);
        return handler.getText();
    }
    
    public static String ubb2imghtml(String ubb) {
        UBBHandler handler = new UBBHandler();
        handler.setBuilderMap(builderMap);
        UBBParser.parse(ubb, handler);
        return handler.getImgText();
    }

    @Deprecated
    public static String ubb2html(String ubb, long postId) {
        UBBHandler handler = new UBBHandler();
        handler.setPostId(postId);
        handler.setBuilderMap(builderMap);

        UBBParser.parse(ubb, handler);

        return handler.getText();
    }

	/**
	 * UBB代码转HTML代码
	 * @param ubb	UBB文本
	 * @param postId	帖子ID
	 * @param floor		帖子所属楼层
	 * @return	HTML文本
	 */
    public static String ubb2html(String ubb, long postId, int floor) {
        UBBHandler handler = new UBBHandler();
        handler.setPostId(postId);
        handler.setFloor(floor);
        handler.setBuilderMap(builderMap);
        UBBParser.parse(ubb, handler);
        return handler.getText();
    }
}
